// 请求地址
const URL = "http://treeadm.mlscd.cn/";
// const URL = "http://47.108.192.57/";
// const URL = "http://192.168.5.11/tree/public/";
//const IMG_URL = "http://blessadm.mlscd.cn/";
const IMG_URL = "http://treeadm.mlscd.cn/";


// 名字 植绿遂林 互联网+
const Title = "植绿遂林——抽红包赢奖金领证书";
const Slogan = "绿化委制发《全民义务植树尽责证书》";
const URL_UL = {
    "js_config": 'api/js-config',//获取分享配置 get
    "userinfo": 'api/userinfo',//通过code 获取用户数据 get
    "auth_url": "api/auth-url",//获取用户授权地址 get
    "upduser": "api/upduser",//绑定战队信息get
    "tree_detail": 'api/tree-detail',//树详情  get
    "tree_interact": "api/tree-interact",//树苗互动 post
    "tree_dynamic": "api/tree-dynamic",//获取动态列表 get
    "sort_all": "api/sort-all",//所有人排行榜 get
    "cert_sort": "api/cert-sort",//证书排行榜 get

    "sort": 'api/sort',//社区排行 get
    "handle_status": 'api/handle-status',//获取是否有资格给好友浇水 get
    "team_add": 'api/team-add',//创建社区 posts
    "red_bonus": 'api/red-bonus',//获取红包 post
    "share": 'api/share',//分享得能量 post
    "message_submit": 'api/message-submit',//提交表单 post
    "tree_rule": 'api/tree-rule',//获取规则说明 get


    "refresh_token": "api/refresh-token",//刷新token get
}

// 用户信息
const USER = function () {
    let arr =
    {
        avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/ZdHZ8vlDjCo7LJggIU7TEpDgt0mpcedXZiaY6mwge03dDR7JlQaUWj17pwn0LXj9oWo8Qd9HLARwalE0Yiav9yCw/132"
        , create_time: "2020-02-10 16:54:20"
        , delete_time: 0
        // , id: 45
        , id: 40184
        , money: 524
        , nickname: "起了一个很长的名字。"
        , openid: "owK-CwKxFDC61fzLfokzuzlqRYPQ"
        , team_id: 12784
        , update_time: "2020-02-10 16:54:20"
    }
    return localStorage.getItem("USER3") ? JSON.parse(localStorage.getItem("USER3")) : arr//;false 
}
// 删除用户信息
const REMOVE_USER = function () {
    localStorage.removeItem('COMMUNITY');
    localStorage.removeItem('USER3');
    localStorage.clear();

}
const TOKEN = function () {
    let arr = 'eyJhbGciOiJzaGEyNTYiLCJ0eXAiOiJKV1QifQ.eyJyc2giOiJhNzUyZmRjN2E5NTM5YzAxODY0ZjgxMDg5ZjY3NTMyMSIsImlhdCI6MTYxNTM1OTMzMiwiZXhwIjoxNjE1MzU5MzkyLCJpc3MiOiJTRF9DTCIsImp0aSI6Imp0aTYwNDg2ZDY1MTU1MmM1OCIsInVzZXJfaWQiOjQwMTg0fQ.MTBlM2Q5NTY3ZWIyOThjN2RjMmE1ZjIxY2UyMDM2MWYxZWU3OTc3Yjg5YTIwOTBmMTYzZjNiMzliMzQ0ODI1Ng';
    return localStorage.getItem("TOKEN") ? localStorage.getItem("TOKEN") : arr//;false 
}
const REFRESH_TOKEN = function () {
    let arr = 'eyJleHAiOjE2MTU5NjQxMzIsImp0aSI6ImE3NTJmZGM3YTk1MzljMDE4NjRmODEwODlmNjc1MzIxIn0.YzExMjNiZDcxYzdkNzcyNmVmY2IyNGZhZTgwZDA5N2JlOGM0MDY0YmZmZGM4NmQ0OTBhMWFmZTg3Zjg1ZTI0Nw';
    return localStorage.getItem("REFRESH_TOKEN") ? localStorage.getItem("REFRESH_TOKEN") : arr//;false 
}
// 公众号信息
const DefaultConfig = function (obj) {
    let url = window.location.href;
    // let url = 'http://tree.mlscd.cn';
    AJAX({
        url: URL_UL.auth_url,
        method: 'get',
        data: {
            url: url,
        },
        success: (res) => {
            if (res.data) {
                obj.success(res.data);
            } else {
                // layer.open({
                //     content: '小小战士排队中,请稍后!'
                //     , time: 1
                //     , skin: 'msg'
                // });
            }
        }
    })
}
// 刷新token
const refresh_token = function (obj) {
    AJAX({
        url: URL_UL.refresh_token,
        method: 'get',
        data: {
            token: TOKEN(),
            refresh_token: REFRESH_TOKEN()
        },
        success: (res) => {
            if (res.code == 200) {
                localStorage.setItem('TOKEN', res.data.token);
                AJAX(obj)
            } else {
                REMOVE_USER();
                GudgeUser({
                    success: function () {
                        AJAX(obj)
                    }
                })
            }
        }, fail: (res) => {
            REMOVE_USER();
            GudgeUser({
                success: function () {
                    AJAX(obj)
                }
            })
        }
    })
};

// 请求
const AJAX = function (objT) {
    let obj = objT;
    obj.urlT = obj.urlT ? obj.urlT : URL;
    obj.url = obj.url ? obj.url : '';
    obj.method = obj.method ? obj.method : 'get';
    obj.data = obj.data ? obj.data : {};
    obj.token = obj.token ? obj.token : false;//是否添加用户token
    obj.success = obj.success ? obj.success : (eve) => { };
    obj.fail = obj.fail ? obj.fail : (eve) => {
        layer.open({
            content: eve.msg
            , time: 1
            , skin: 'msg'
        });
    };
    obj.data.Time = Date.parse(new Date());
    if (obj.token) {
        obj.data.token = TOKEN();
    }
    $.ajax({
        type: obj.method,
        url: obj.urlT + obj.url,
        data: obj.data,
        success: function (ret) { //成功的回调函数
            if (ret.code == 200) {
                obj.success(ret)
            } else if (ret.code == 203) {//token过期
                // refresh_token(objT)
            } else {
                obj.fail(ret)
            }
        },
        error: function (e) {
            console.log("error" + '请求错误');
        }
    });

}
//  获取URL?号后面的参数
const GetRequest = function () {
    var url = decodeURI(location.search); //获取url中"?"符后的字串  
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
};

const community = function () {//获取用户社区
    AJAX({
        url: URL_UL.sort,
        method: 'get',
        data: {
            user_id: USER().id,
            page: 1,
            limit: 100,
        },
        success: (res) => {
            localStorage.setItem('HIDE', res.data.hide);
            localStorage.setItem('MY_ENERGY', res.data.my_energy);
            if (res.data.team) {
                return localStorage.setItem('COMMUNITY', res.data.team.id);
            } else {
                establish()
            }
        }, fail: (res) => { }
    })
};
const establish = function () {//创建社区
    AJAX({
        url: URL_UL.team_add,
        method: 'post',
        data: {
            user_id: USER().id,
            title: USER().nickname,
        },
        success: (res) => {
            console.log(res)
            if (res.code == 200) {
                community();
            }
        }, fail: (res) => { }
    })
};

//判断是否授权
const GudgeUser = function (obj) {
    obj = obj ? obj : {
        success: function () {
            return false;
        }
    };
    if (!USER()) {//验证用户信息
        if (GetRequest().code) {
            localStorage.setItem('CODE', GetRequest().code);
            let data = {
                code: localStorage.getItem("CODE"),
            }
            if (GetRequest().community_id && GetRequest().community_id > 0) {
                data.team_id = GetRequest().community_id;
            }
            AJAX({
                url: URL_UL.userinfo,
                method: 'get',
                data: data,
                success: (res) => {
                    if (res.data) {
                        localStorage.setItem('TOKEN', res.data.token.token);
                        localStorage.setItem('REFRESH_TOKEN', res.data.token.refresh_token);
                        localStorage.setItem('USER3', JSON.stringify(res.data))
                        setTimeout(() => {
                            obj.success();
                        }, 500)
                        if (!GetRequest().community_id || GetRequest().community_id == null) {
                            community();
                        }
                    }
                }
            })
        } else {
            DefaultConfig({
                success: function (eve) {
                    window.location.href = eve;
                }
            });
        }
    } else {
        obj.success();
        if (GetRequest().community_id && GetRequest().community_id > 0) {
            AJAX({
                url: URL_UL.upduser,
                method: 'get',
                data: {
                    id: USER().id,
                    team_id: GetRequest().community_id//战队id
                },
                success: (res) => {
                    if (res.data) {
                    }
                }, fail: (res) => { }
            })
        } else {
            community();
        }
    }


    $.fn.ImgZoomIn = function () {
        let src = $(this).attr('src');
        $('#ImgZoomInImage').attr('src', '../images/1.png');
        bgstr = '<div id="ImgZoomInBG" onclick=$(\'#ImgZoomInBG\').hide();$(\'#ImgZoomInImage\').hide(); style=" background:#000000; filter:Alpha(Opacity=70); opacity:0.7; position:fixed; left:0; top:0; z-index:10000; width:100vw; height:100vh; display:none;"></div>';
        imgstr = '<img id="ImgZoomInImage" src="' + src + '" alt="加载中！" style="cursor:pointer; display:none; position:absolute; z-index:10001;" />';
        if ($('#ImgZoomInBG').length < 1) {
            $('body').append(bgstr);
        }
        if ($('#ImgZoomInImage').length < 1) {
            $('body').append(imgstr);
        }
        else {
            $('#ImgZoomInImage').attr('src', src);
        }
        $('#ImgZoomInImage').css('left', $(window).scrollLeft() + ($(window).width() - $('#ImgZoomInImage').width()) / 2);
        $('#ImgZoomInImage').css('top', $(window).scrollTop() + ($(window).height() - $('#ImgZoomInImage').height()) / 2);
        $('#ImgZoomInBG').show();
        $('#ImgZoomInImage').show();
        $("#ImgZoomInImage").click(function () {
            $('#ImgZoomInBG').hide();
            $('#ImgZoomInImage').hide();
        });
    }
}
